﻿using System;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using JQueryControlExtenders.BaseExtender;

namespace JQueryControlExtenders.Extenders
{
  /// <summary>
  /// Extends an ASP.NET TextBox with a JQuery Calendar
  /// </summary>
  [TargetControlType(typeof(TextBox))]
    public class TextBoxCalendarExtender : ExtenderControl
  {
    /// <summary>
    /// JQuery DateFormat
    /// </summary>
    public string DateFormat { get; set; }

    /// <summary>
    /// Set a minimum selectable date via a Date object, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '-1y -1m'), or null for no limit.
    /// </summary>
    public string MinDate { get; set; }

    /// <summary>
    /// Set a maximum selectable date via a Date object, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +1w'), or null for no limit.
    /// </summary>
    public string MaxDate { get; set; }

    /// <summary>
    /// Allows you to change the month by selecting from a drop-down list. You can enable this feature by setting the attribute to true.
    /// </summary>
    public bool ChangeMonth { get; set; }

    /// <summary>
    /// Allows you to change the year by selecting from a drop-down list. You can enable this feature by setting the attribute to true.
    /// </summary>
    public bool ChangeYear { get; set; }

    public bool UseDisposable { get; set; }

    protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
    {
        var cal = new JQueryScriptControlDescriptor("datepicker", targetControl.ClientID);
        if(UseDisposable)
            cal.Suffix = ".Disposable()";
        if (!string.IsNullOrEmpty(DateFormat))
            cal.AddProperty("dateFormat", DateFormat);
        if (!string.IsNullOrEmpty(MinDate))
            cal.AddProperty("minDate", MinDate);
        if (!string.IsNullOrEmpty(MaxDate))
            cal.AddProperty("maxDate", MaxDate);
        if (ChangeMonth)
            cal.AddProperty("changeMonth", ChangeMonth);
        if (ChangeYear)
            cal.AddProperty("changeYear", ChangeYear);
        yield return cal;
    }

    protected override IEnumerable<ScriptReference> GetScriptReferences()
    {
        if(UseDisposable)
            yield return new ScriptReference("JQueryControlExtenders.BaseExtender.jquery-disposable.js", "JQueryControlExtenders");
        else
            yield break;
    }
  }
}